<?php


/**
 * 情人节活动后端订单流程
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   allen.qiang  <allen.qiang@fistforward.com> 2015-02-02 allen.qiang
 */

quanli_require('module/base/bill/billSearch.class.php');

class pa_sfImporting {

    public function execute() {

        global $G_PGLOB;

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: //显示顺丰出仓单导入页面
                    $this->ccdImportingTpl();
                    break;
                case 2: //显示顺丰配送完成单导入页面
                    $this->wcdImportingTpl();
                    break;
                case 3: //顺丰出仓单导入
                    $this->ccdImporting();
                    break;
                case 4: //顺丰配送完成单导入
                    $this->wcdImporting();
                    break;
                default:
                    $G_PGLOB->smarty->assign('msg', '参数错误');
                    $G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }

    /**
     * 显示顺丰出仓单导入页面
     */
    private function ccdImportingTpl() {
        global $G_PGLOB;

        $G_PGLOB->smarty->display('bill/SF_ccdImporting.tpl');
    }

    /**
     * 显示顺丰配送完成单导入页面
     */
    private function wcdImportingTpl() {
        global $G_PGLOB;

        $G_PGLOB->smarty->display('bill/SF_wcdImporting.tpl');
    }


    /**
     * 顺丰出仓单导入
     */
    private function ccdImporting() {
        global $G_PGLOB;

        quanli_include('module/base/common/quanliFile.class.php');
        quanli_include('module/base/bill/billEdit.class.php');
        //实例日志类
        $quanliLog = new quanliLog();

        //实例文件
        $quanliFile = new quanliFile;

        /**
         * 得到上传文件，并保存为新文件在服务器上
         */
        $upload_result_array = $quanliFile->get_upload_file( 'SF' );
        //echo $upload_result_array['message'];
        if ($upload_result_array['code'] != 200) {
            die('<script>parent.customerSms.importMsg("' . $upload_result_array['message'] . '", 0);</script>');
        }


        /**
         * 读取Excel文件内容
         */
        $data_array = $quanliFile->readerExcel1( $upload_result_array['data'] ,1);
        if ($data_array['code'] != 200) {
            die('<script>parent.customerSms.importMsg("' . $data_array['message'] . '", 0);</script>');
        }
        //取得待发数据总数量
        $data_cnt=count($data_array['data']);
        if ($data_cnt > 2000) {
            die('<script>parent.message.importMsg("每次最多可导入2000条数据", 0);</script>');
        }

        /**
         * ======================================
         * 导入出库单
         * ======================================
         */
        $ret_message = "导入成功";
        //start update by  guojun.song 2015-5-4  102-MK-AC-1012-MCAKE-EC母亲节活动

        $line = 1;
        $temp_arr = array();
        //array_shift($data_array['data']);
        if(count($data_array['data']) == 1){
            die("<script>parent.customerSms.importMsg('导入单中没有数据',0);</script>");
        }
        foreach( $data_array['data'] as &$bill_array) {
            if(!isset($bill_array[0])){
                die("<script>parent.customerSms.importMsg('第".$line."行订单号有缺失',0);</script>");
            }
            if(!isset($bill_array[1])){
                die("<script>parent.customerSms.importMsg('第".$line."行运单号有缺失',0);</script>");
            }
            if (isset($bill_array[0]) && !empty($bill_array[0]) && !strstr($bill_array[0],"订单")) {

                //查询订单
                $bill_master = new entity('bill_master');
                $bill_master->bill_code = $bill_array[0];
                //$bill_master->send_date = date('Y-m-d','+1 day');
                if($G_PGLOB->DBCA->fetch($bill_master)){

                    if($bill_master->bill_status > 0){
                        if($bill_master->bill_status>=5 && $bill_master->external_bill_no!=""){
                            if($ret_message=="导入成功")
                                $ret_message="";
                            if($bill_master->bill_status==5)
                                $ret_message = $ret_message . $bill_array[0] . '-->订单已下车，不用重复导入<br />';
                            else if($bill_master->bill_status==7)
                                $ret_message = $ret_message . $bill_array[0] . '-->订单已完成，不能导入<br />';


                        }else{
                            $bill_master->external_bill_no = $bill_array[1];//顺丰运单号
                            $bill_master->bill_status =5;//改变状态为已上车
                            $G_PGLOB->DBCA->save($bill_master);

                            $count = 0;

                            if(in_array($bill_array[0],$temp_arr)){
                                $ret_message = $ret_message . $bill_array[0] . '-->订单重复，请检查<br />';
                                die("<script>parent.customerSms.importMsg('".$ret_message."',0);</script>");
                            }else{
                                //if($bill_master->bill_status>=4 && $bill_master->external_bill_no!=""){
                                //保存日志
                                $log_array = array(
                                    'logCategoryID' => 1,
                                    'logItem' => $bill_master->bill_no,
                                    'extend_1' => '系统更改订单状态',
                                    'extend_2' => '已下车，顺丰运单号：'. $bill_array[1],
                                    'extend_5' => 5,
                                    'operator' => $G_PGLOB->currentUserName
                                );
                                $quanliLog->saveLogInfo( $log_array );

                                //保存订单轨迹
                                $operator = $G_PGLOB->currentUserID;
                                $content = '您的订单已配送站收货';
                                $billEdit = new billEdit( $G_PGLOB->DBCA);
                                 $billEdit->addBillTrace( $bill_master->bill_no, $operator, $content );
                                // }
                                $count++;

                                array_push($temp_arr,$bill_array[0]);
                            }


                        }
                    }else{
                        $ret_message =  $bill_array[0] . '-->订单未确认<br />';
                    }


                }else{
                    if($ret_message=="导入成功")
                        $ret_message="";
                    $ret_message = $ret_message . $bill_array[0] . '-->订单信息不存在<br />';
                }
            }
            $line++;

        }
        if(isset($ret_message) && !empty($ret_message) && $ret_message=="导入成功"){
            die("<script>parent.customerSms.importMsg('".$ret_message."   一共导入".$count."条.',0);</script>");
        }/*else if(isset($count) && $count ==0){
            die("<script>parent.customerSms.importMsg('不要重复导入',0);</script>");
        }*/elseif($count > 0){
            die("<script>parent.customerSms.importMsg('"."导入成功   一共导入".$count."条.',0);</script>");
        }
        die("<script>parent.customerSms.importMsg('".$ret_message."',0);</script>");
        //end update by  guojun.song 2015-5-4  102-MK-AC-1012-MCAKE-EC母亲节活动
    }

    /**
     * 顺丰配送完成单导入
     */
    private function wcdImporting() {
        global $G_SHOP;

        quanli_include('module/base/common/quanliFile.class.php');
        quanli_include('module/base/bill/billEdit.class.php');
        //实例日志类
        $quanliLog = new quanliLog();

        //实例文件
        $quanliFile = new quanliFile();

        /**
         * 得到上传文件，并保存为新文件在服务器上
         */
        $upload_result_array = $quanliFile->get_upload_file( 'SF' );
        if ($upload_result_array['code'] != 200) {
            die('<script>parent.customerSms.importMsg("' . $upload_result_array['message'] . '", 0);</script>');
        }

        /**
         * 读取Excel文件内容
         */
        $data_array = $quanliFile->readerExcel1( $upload_result_array['data'] );
        if ($data_array['code'] != 200) {
            die('<script>parent.customerSms.importMsg("' . $data_array['message'] . '", 0);</script>');
        }
        //取得待发数据总数量
        $data_cnt=count($data_array['data']);
        if ($data_cnt > 2000) {
            die('<script>parent.message.importMsg("每次最多可导入2000条数据", 0);</script>');
        }

        /**
         * ======================================
         * 导入配送完成单
         * ======================================
         */
        $ret_message = "导入成功";
        //start update by  guojun.song 2015-5-4  102-MK-AC-1012-MCAKE-EC母亲节活动
        if($data_cnt <= 1 && $data_array['data'][0][0] == '运单号'){
            die("<script>parent.customerSms.importMsg('导入单中没有数据',0);</script>");
        }

        $temp_arr = array();
        foreach( $data_array['data'] as &$bill_array) {

            if (isset($bill_array[0]) && !empty($bill_array[0]) && !strstr($bill_array[0],"号")) {

                //根据顺丰运单号查询订单信息
                $sql_str="SELECT * FROM bill_master WHERE send_date >= '2015-05-05' AND send_date <= '2015-07-31' AND external_bill_no=?";//echo $sql_str;die;
                //根据订单编号查询订单信息
                //$sql_str="SELECT * FROM bill_master WHERE send_date >= '2015-02-09' AND send_date <= '2015-05-17' AND bill_code=?";
                $item_array = &$G_SHOP->DBCA->stmt_query($sql_str,'s',$bill_array[0]);

                if($item_array){
                    $bill_no=$item_array[0]->bill_no;//订单号

                    if($item_array[0]->bill_status==7){
                        if($ret_message=="导入成功")
                            $ret_message="";
                        $ret_message = $ret_message . $bill_array[0] . '-->订单已完成，不用重复导入<br />';
                    }else{
                        //查询订单支付信息
                        $search_sql = 'SELECT * FROM bill_pay  WHERE bill_no = ?';
                        $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, 's', $bill_no);

                        if($temp_item_array) {
                            foreach ($temp_item_array as &$bill_pay_obj) {
                                $bill_distribution_obj = new entity('bill_distribution_checking');
                                $bill_distribution_obj->bill_no = $bill_no;
                                $bill_distribution_obj->creator = $G_SHOP->currentUserID;
                                $bill_distribution_obj->payTypeID = $bill_pay_obj->payTypeID;
                                $bill_distribution_obj->payTypeName = $bill_pay_obj->payTypeName;
                                $bill_distribution_obj->type = $bill_pay_obj->type;
                                $bill_distribution_obj->pay_amount = $bill_pay_obj->pay_amount;
                                $bill_distribution_obj->deduct_amount = $bill_pay_obj->deduct_amount;
                                $bill_distribution_obj->pay_code = $bill_pay_obj->pay_code;
                                $bill_distribution_obj->pay_time = $bill_pay_obj->pay_time;
                                $bill_distribution_obj->paid_confirm_status = $bill_pay_obj->paid_confirm_status;
                                $bill_distribution_obj->paid_confirmer = $bill_pay_obj->paid_confirmer;
                                $bill_distribution_obj->paid_confirm_time = $bill_pay_obj->paid_confirm_time;
                                $bill_distribution_obj->payment_method_id = $bill_pay_obj->payment_method_id;
                                $bill_distribution_obj->payment_method_name = $bill_pay_obj->payment_method_name;
                                $bill_distribution_obj->payment_method_type = $bill_pay_obj->payment_method_type;
                                $bill_distribution_obj->pos_serial_number = "";
                                $bill_distribution_obj->pos_device_number = "";

                                if($bill_pay_obj->type == 2){
                                    $bill_distribution_obj->receipt_amount = $item_array[0]->bill_online_money;
                                    $bill_distribution_obj->cash_card_number = "";
                                }else if($bill_pay_obj->type == 7){
                                    $bill_distribution_obj->receipt_amount = 0;
                                    $bill_distribution_obj->cash_card_number = $bill_pay_obj->cash_card_number;
                                }else{
                                    $bill_distribution_obj->receipt_amount = $bill_pay_obj->pay_amount;
                                    $bill_distribution_obj->cash_card_number = "";
                                }

                                //保存配送对账表
                                $G_SHOP->DBCA->save( $bill_distribution_obj );

                              /*  if(in_array($bill_array[0],$temp_arr)){
                                    $ret_message =  $bill_array[0] . '-->运单号重复，请检查<br />';
                                    die("<script>parent.customerSms.importMsg('".$ret_message."',0);</script>");
                                }else{*/
                                    $log_array = array(
                                        'logCategoryID' => 1,
                                        'logItem' => $bill_no,
                                        'extend_1' => '配送站已收货',
                                        'extend_2' => '顺丰运单号：'.$bill_array[0] ,
                                        'extend_5' => 5,
                                        'operator' => $G_SHOP->currentUserName,
                                        //'createTime'=>  '2015-05-05 10:21:08'
                                    );
                                    $quanliLog->saveLogInfo( $log_array );

                                    $log_array = array(
                                        'logCategoryID' => 1,
                                        'logItem' => $bill_no,
                                        'extend_1' => '订单已结算',
                                        'extend_2' => $bill_distribution_obj->payTypeName.' '.$bill_distribution_obj->receipt_amount.'元 已结算',
                                        'extend_5' => 7,
                                        'operator' => $G_SHOP->currentUserName
                                    );
                                    $quanliLog->saveLogInfo( $log_array );

                              /*      array_push($temp_arr,$bill_array[0]);
                                }*/

                            }
                        }

                        //修改订单主表配送对账字段，保存配送对账备注
                        $bill_master = new entity('bill_master');
                        $bill_master->bill_no = $bill_no;
                        if($G_SHOP->DBCA->fetch($bill_master)){
                            $bill_master->is_reconciliation = 1;
                            $bill_master->bill_status = 7;
                            $bill_master->bill_received_money += $bill_master->bill_receivable_money;
                            $bill_master->bill_receivable_money = 0;
                            $bill_master->bill_checking_remark = "";
                            $G_SHOP->DBCA->save( $bill_master );
                        }

                        //保存订单结算时状态，应收金额到订单结算临时表
                        $bill_temp_obj = new entity('bill_distribution_temp');
                        $bill_temp_obj->bill_no = $bill_no;
                        if(!$G_SHOP->DBCA->fetch($bill_temp_obj)){
                            $bill_temp_obj->creator = $G_SHOP->currentUserID;
                            $bill_temp_obj->bill_receivable_money = $item_array[0]->bill_receivable_money;
                            $bill_temp_obj->bill_received_money = $item_array[0]->bill_received_money;
                            $bill_temp_obj->bill_status = $item_array[0]->bill_status;
                            $G_SHOP->DBCA->save( $bill_temp_obj );
                        }




                        //已完成的订单表
                        $bill_finish_obj = new entity('bill_finish');
                        $bill_finish_obj->bill_no = $bill_no;
                        if(!$G_SHOP->DBCA->fetch($bill_finish_obj)){
                            $bill_finish_obj->creator = $G_SHOP->currentUserID;
                            //把完成对账的订单号存入bill_finish表中
                            $G_SHOP->DBCA->save( $bill_finish_obj );
                        }

                        /*=======================================================
                       * 保存订单轨迹 start
                       * ======================================================
                       */

                        $operator = $G_SHOP->currentUserID;
                        $content = '您的订单已结算';

                        $billEdit = new billEdit( $G_SHOP->DBCA);
                        $billEdit->addBillTrace( $bill_no, $operator, $content );

                        /*=======================================================
                         * 保存订单轨迹 end
                         * ======================================================
                         */

                    }

                }else{
                    if($ret_message=="导入成功")
                        $ret_message="";
                    $ret_message = $ret_message . $bill_array[0] . '-->订单信息的运单号不存在 请先导入运单号<br />';
                }
            }
        }
        //end update by  guojun.song 2015-5-4  102-MK-AC-1012-MCAKE-EC母亲节活动
        die("<script>parent.customerSms.importMsg('".$ret_message."',0);</script>");

    }


}
